package com.zzh.controller;

import com.github.pagehelper.PageInfo;
import com.zzh.domain.PromotionAd;
import com.zzh.domain.PromotionAdVO;
import com.zzh.domain.PromotionSpace;
import com.zzh.domain.ResponseResult;
import com.zzh.service.PromotionAdService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.io.File;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;
import java.util.UUID;

@RestController
@RequestMapping("/PromotionAd")
public class PromotionAdController {

    @Autowired
    private PromotionAdService promotionAdService;

    @RequestMapping("/findAllPromotionAdByPage")
    public ResponseResult findAllPromotionAdByPage(PromotionAdVO promotionAdVO) {
        PageInfo<PromotionAd> pageInfo = promotionAdService.findAllPromotionAdByPage(promotionAdVO);
        ResponseResult result = new ResponseResult(true, 200, "成功响应", pageInfo);
        return result;
    }

    @RequestMapping("/PromotionAdUpload")
    public ResponseResult PromotionAdUpload(@RequestParam("file") MultipartFile file, HttpServletRequest request) {
        try {
            if (file.isEmpty()) {
                throw new RuntimeException("文件是null的");
            }
            String realPath = request.getServletContext().getRealPath("/");
            String webappPath = realPath.substring(0, realPath.indexOf("zzh_edu_ssm_web"));
            String fileName = file.getOriginalFilename();
            String newFileName = UUID.randomUUID() + fileName.substring(fileName.lastIndexOf("."));
            String uploadPath = webappPath + "upload//";
            File filePath = new File(uploadPath, newFileName);
            if (!filePath.getParentFile().exists()) {
                filePath.getParentFile().mkdirs();
            }
            file.transferTo(filePath);
            Map<String, String> map = new HashMap<>();
            map.put("fileName", newFileName);
            map.put("filePath", "http://localhost:8080" + "/upload/" + newFileName);
            ResponseResult result = new ResponseResult(true, 200, "上传文件成功", map);
            return result;
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }

    @RequestMapping("/saveOrUpdatePromotionAd")
    public ResponseResult saveOrUpdatePromotionAd(@RequestBody PromotionAd promotionAd) {
        ResponseResult result = null;
        if (promotionAd.getId() == null) {
            Date date = new Date();
            promotionAd.setCreateTime(date);
            promotionAd.setUpdateTime(date);
            promotionAdService.savePromotionAd(promotionAd);
            result = new ResponseResult(true, 200, "成功响应", null);
        }else {
            promotionAd.setUpdateTime(new Date());
            promotionAdService.updatePromotionAd(promotionAd);
            result = new ResponseResult(true, 200, "成功响应", null);
        }
        return result;
    }

    @RequestMapping("/findPromotionAdById")
    public ResponseResult findPromotionAdById(@RequestParam int id) {
        PromotionAd promotionAd = promotionAdService.findPromotionAdById(id);
        return new ResponseResult(true, 200, "成功响应", promotionAd);
    }

    @RequestMapping("/updatePromotionAdStatus")
    public ResponseResult updatePromotionAdStatus(@RequestParam int id, @RequestParam int status) {
        if (status == 1) {
            promotionAdService.updatePromotionAdStatus(id, status);
        }else {
            promotionAdService.updatePromotionAdStatus(id, 0);
        }
        //保存修改后的状态
        Map<String, Integer> map = new HashMap<>();
        map.put("status", status);
        return new ResponseResult(true, 200, "成功响应", map);
    }
}
